//
//  BGAddReportViewController.m
//  bigMall
//
//  Created by LTOVE on 16/9/22.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGAddReportViewController.h"
#import "BGcollectionViews.h"
//#import "Bgcoll"
#import "BGAddReportViewCell.h"

#import "BGAddReportModel.h"
#import "BGChoosePicTableViewController.h"
#import "BGUpLoadFileTool.h"
#import "LTOVEDynamicCreakButton.h"
#import "MBProgressHUD.h"
#import "MBProgressHUD+MJ.h"
#import "MJExtension.h"

#import "BGDateResult.h"
#import "BGSellerOrderStepModel.h"
@interface BGAddreParamModel : NSObject

@property (nonatomic,copy)NSString *orderId;
@property (nonatomic,copy)NSString *action;
@property (nonatomic,copy)NSString *fileIds;
@property (nonatomic,copy)NSString *filePaths;
@property (nonatomic,copy)NSString *freight;
@property (nonatomic,copy)NSString *finalPrice;
@property (nonatomic,copy)NSString *finalNumber;
@end
@interface BGAddReportViewController ()<BGAddReportViewCellDelegate,BGChoosePicViewControllerDelegate,dynamicButtonDelegate>{
    
    NSMutableArray *dataArray;
    CGFloat cellHeigh;
}
@property (nonatomic,weak)LTOVEDynamicCreakButton *views;
@property (nonatomic,copy)BGAddreParamModel *param;
@end




@implementation BGAddreParamModel

-(NSString *)action
{
    return @"postReport";
}

@end
#pragma mark --⬆️请求参数模型
@implementation BGAddReportViewController

- (BGAddreParamModel *)param
{
    if (!_param) {
        _param = [BGAddreParamModel new];
//        _param.orderId = @"213";
    }
    return _param;
}
- (void)setOrderId:(NSString *)orderId
{
    _orderId = orderId;
    self.param.orderId = orderId;
}
#pragma mark --cell内容改变代理
- (void)textFildDidEndEditingWith:(BGAddReportModel *)model
{
//    [dataArray replaceObjectAtIndex:model.indexPath.row withObject:model];
    BGAddReportModelLists *itemModel = (BGAddReportModelLists *)model;
    CGFloat Value = [itemModel.valueStr floatValue];
    switch (itemModel.indexPath.row) {
        case 0:
            self.param.finalNumber = [NSString stringWithFormat:@"%.3f",Value];
            break;
        case 1:
            self.param.freight = [NSString stringWithFormat:@"%.2f",Value];
            break;
        case 2:
            self.param.finalPrice = [NSString stringWithFormat:@"%.2f",Value];
            break;
            
        default:
            break;
    }
    
    
    
//    [self.tableView reloadData];
//    GWLog(@"%@",self.param.keyValues);
//    GWLog(@"%ld",(long)itemModel.indexPath.row);
}

- (void)viewWillAppear:(BOOL)animated
{
    
    [self addDynamicBtn];
//    self.views.hidden = !self.status;
}
- (void)viewWillDisappear:(BOOL)animated
{
    [self.views removeFromSuperview];
}

- (void)dynamicButtonClick:(UIButton *)btn
{
    GWLog(@"btnCilck");
    [MBProgressHUD showMessage:@"提交中"];
    for (BGAddReportModel *model in dataArray) {
        if ([model isKindOfClass:[BGAddReportModelImages class]]) {
            BGAddReportModelImages *models = (BGAddReportModelImages *)model;
            GWLog(@"%@",models.photosArray);
            
            [BGUpLoadFileTool upLoadPicWithImageDataArray:models.photosArray success:^(NSArray *filePathArray, NSArray *idStrArr) {
                
                NSMutableString *filePathStr = [NSMutableString string];
                NSMutableString *fileIdStr = [NSMutableString string];
                for (NSString *str  in idStrArr) {
                    [fileIdStr appendFormat:@"%@,",str];
                }
                for (NSString *str  in filePathArray) {
                    [filePathStr appendFormat:@"%@, ",str];
                }
                self.param.fileIds = fileIdStr;
                self.param.filePaths = filePathStr;
                
                
                [BGDateResult buyerOrderStepWithParamDic:self.param.keyValues success:^(NSObject *result) {
                    
                    BGSellerOrderStepModel *resultModel = (BGSellerOrderStepModel *)result;
                    GWLog(@"%@",resultModel.keyValues);
                    GWLog(@"%@",resultModel.msg);
                    [MBProgressHUD hideHUD];
                    
                    [self alertViewWithMessage:resultModel.msg sureBtnClick:^{
                        if ([resultModel.status isEqualToString:@"1"]) {
                            [self.navigationController popViewControllerAnimated:YES];
                        }
                        GWLog(@"hao");
                    }];
                    
//                    [MBProgressHUD showSuccess:@"上传成功"];
                    
                } failure:^(NSError *error) {
                    [MBProgressHUD hideHUD];
                }];
                
                

                
                
            } failure:^(NSError *error) {
                
                [MBProgressHUD hideHUD];
            }];
            
        }
    }
}
#pragma mark -- 弹窗
/**
 *  弹窗
 *
 *  @param message      message description
 *  @param sureBtnClick sureBtnClick description
 */
- (void)alertViewWithMessage:(NSString *)message sureBtnClick:(void(^)())sureBtnClick
{
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sure = [UIAlertAction actionWithTitle:@"好" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        if (sureBtnClick) {
            sureBtnClick();
        }
        
    }];
    [alertVc addAction:sure];
    [self presentViewController:alertVc animated:YES completion:nil];
}

- (void)addDynamicBtn
{
    NSDictionary *dic = @{
                          NSFontAttributeName:Font34,
                          NSForegroundColorAttributeName:[UIColor whiteColor]
                          };
    //    NSLog(@"%@",_btnArray);
    CGFloat x = BGButtonCellHeigh;
    CGFloat heigh = screenH - x;
    NSArray *_btnArray = @[@"提交验收报告"];
    LTOVEDynamicCreakButton *view = [LTOVEDynamicCreakButton buttonViewWithFrame:CGRectMake(0, heigh, screenW, BGButtonCellHeigh) titleDataArray:_btnArray andCol:(int)_btnArray.count andEdgInset:UIEdgeInsetsMake(10,BGMaginLeft , 10, BGMaginLeft) andMaginX:10 andMaginY:0 andBtnHeigh:BGMessageBtn andBtnNorColor:read_ andBtnHeighColor:read_ andRadius:7 andFont:dic];
    //    LTOVEDynamicCreakButton *view = [LTOVEDynamicCreakButton buttonViewWithFrame:CGRectMake(0,heigh, screenW, BGButtonCellHeigh) titleDataArray:self.btnArray andEdgInset:UIEdgeInsetsMake(10, BGMaginLeft, 10, BGMaginLeft) andMaginX:0 andMaginY:0 andBtnHeigh:BGMessageBtn andBtnNorColor:read_ andBtnHeighColor:read_ andRadius:7 andFont:dic];
    //    view.delegate = self;
    //    view.y = 300;
    self.views = view;
    //    self.views.hidden = YES;
    view.delegate = self;
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, x, 0);
    [BGKeyWindow addSubview:view];
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    BGcollectionViews *vc = [[BGcollectionViews alloc]init];
//    [vc registerClass:[collectionViewCells class] forCellWithReuseIdentifier:@"cell"];
//    vc.backgroundColor = lightGray_;
//    vc.dataArray = nil;
////    [vc reloadData];
//    [self.view addSubview:vc];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [self initData];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    GWLog(@"ssss");
}
- (void)initData
{
    dataArray = [NSMutableArray array];
    BGAddReportModelLists *OneListModel = [BGAddReportModelLists new];
    OneListModel.title = @"收货重量(千吨)";
    OneListModel.color = textGrayColor;
    OneListModel.cellheigh = 64;
    BGAddReportModelLists *TwoListModel = [BGAddReportModelLists new];
    TwoListModel.title = @"运费价格(元/千吨)";
    TwoListModel.color = read_;
    TwoListModel.cellheigh = 64;
    BGAddReportModelLists *threeListModel = [BGAddReportModelLists new];
    threeListModel.color = read_;
    threeListModel.title = @"最终成交价(元)";
    threeListModel.cellheigh = 64;
    [dataArray addObject:OneListModel];
    [dataArray addObject:TwoListModel];
    [dataArray addObject:threeListModel];
    BGAddReportModelLineCell *cellLineModel = [BGAddReportModelLineCell new];
    cellLineModel.cellheigh = 10;
    [dataArray addObject:cellLineModel];
    BGAddReportModelTitle *titleModel = [BGAddReportModelTitle new];
    titleModel.title = @"验收报告";
    titleModel.cellheigh = 64;
    [dataArray addObject:titleModel];
    BGAddReportModelImages *photosModel = [BGAddReportModelImages new];
    photosModel.photosArray = @[];
//    cellHeigh = 200;
    photosModel.cellheigh = 200;
    [dataArray addObject:photosModel];
    
    [self.tableView reloadData];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    if (dataArray.count - 1 == indexPath.row) {
//        return cellHeigh;
//    }else{
//        return 64;
//    }
    BGAddReportModel *model = dataArray[indexPath.row];
    return model.cellheigh;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BGAddReportViewCell *cell = [BGAddReportViewCell cellWithTableView:tableView];
    BGAddReportModel *model = dataArray[indexPath.row];
    model.indexPath = indexPath;
    cell.model = model;
    cell.delegate = self;
    // Configure the cell...
    
    return cell;
}

- (void)lastCollectionItemClick
{
    BGChoosePicTableViewController *vc = [[BGChoosePicTableViewController alloc]init];
    UIBarButtonItem *bac = [UIBarButtonItem new];
    bac.title = @"";
    self.navigationItem.backBarButtonItem = bac;
    vc.delegate = self;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)choosedPicWithPhotosArray:(NSArray *)photoArray cellHeigh:(CGFloat)heigh
{
    BGAddReportModelImages *imageModel = [dataArray lastObject];
    imageModel.photosArray = photoArray;
    imageModel.cellheigh = heigh;
//    GWLog(@"------>%lf",cellHeigh);
    [dataArray replaceObjectAtIndex:dataArray.count - 1 withObject:imageModel];
//    dataArray = [NSMutableArray arrayWithArray:photoArray];
//    cellHeigh = heigh;
    [self.tableView reloadData];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self.tableView endEditing:YES];
}
@end
